const path = require("path")
const Koa = require("koa")
const render = require("koa-art-template")
const static = require("koa-static")
const mount = require("koa-mount")
const bodyParser = require("koa-bodyparser")
const router = require("./firstRouter")

const app = new Koa();

app.use(bodyParser());

render(app, {
    root: path.join(__dirname, 'view'),
    extname: ".html",
    debug: process.env.NODE_ENV !== 'production'
})

app.use(
    mount("/public", static(path.join(__dirname, "static")))
)

app.use(async (ctx, next) => {
    try {
        await next()
    } catch (err) {
        ctx.status = err.statusCode || err.status || 500;
        ctx.body = {
            message: err.message
        };
    }
})

app.use(router.routes()).use(router.allowedMethods())

app.listen(8888, () => {
    console.log("starting at port 8888!");
})